import {defineStore} from "pinia";

import {getAutocompleteByBatch} from "@/api/afterSale/order/AfterSaleOrderLineController";
import {
    getAfterSaleConfigAllGroupByMap,
    getAfterSaleOrderEnum,
    getSystemAddressEnumTree
} from "@/api/system/SystemConfigService";
import AfterSaleConfigAllDTO from "@/api/system/dto/AfterSaleConfigAllDTO";
import AddressTreeDTO from "@/api/system/dto/AddressTreeDTO";
import ItemLineInfoDTO from "@/api/afterSale/order/dto/ItemLineInfoDTO";
import AfterSaleOrderEnumDTO from "@/api/system/dto/AfterSaleOrderEnumDTO";


/**
 * 仓库类型
 *
 * @author 徐寿春
 * 2023/4/7 16:27
 */
export interface AfterSaleConfigStore {
    // 售后配置信息
    afterSaleConfigInfo: AfterSaleConfigAllDTO
    // 地址信息
    addressTree: AddressTreeDTO []
    // 物料信息
    itemLineInfos: ItemLineInfoDTO []
    // 售后状态信息
    afterSaleOrderEnumInfo: AfterSaleOrderEnumDTO
}


/**
 *
 *  应用当前应用仓库
 */
export const useAfterSaleConfigStore = defineStore('afterSaleConfig', {

        state: () => ({
            afterSaleConfigInfo: new AfterSaleConfigAllDTO(),
            addressTree: [],
            afterSaleOrderEnumInfo: new AfterSaleOrderEnumDTO(),
            itemLineInfos: []

        } as AfterSaleConfigStore),


        actions: {
            /**
             * 获取售后配置
             */
            async getAfterSaleConfig(): Promise<AfterSaleConfigStore> {

                // 工单状态信息
                if (!this.afterSaleOrderEnumInfo || this.addressTree.length == 0) {

                    // 物料信息
                    getAutocompleteByBatch().then(res => {
                        this.itemLineInfos = res
                    })

                    // 后台配置信息
                    await getAfterSaleConfigAllGroupByMap().then(resp => {
                        this.afterSaleConfigInfo = resp
                    })

                    // 工单枚举信息
                    await getAfterSaleOrderEnum().then(resp => {
                        this.afterSaleOrderEnumInfo = resp
                    })

                    // 地址
                    await getSystemAddressEnumTree().then(resp => {
                        this.addressTree = resp
                    })
                }


                return {
                    afterSaleConfigInfo: this.afterSaleConfigInfo,
                    addressTree: this.addressTree,
                    afterSaleOrderEnumInfo: this.afterSaleOrderEnumInfo,
                    itemLineInfos: this.itemLineInfos

                }
            },
        }
    }
)

